package com.app.studyroom.mapper;

import com.app.studyroom.api.model.ssrGet.ClassroomResponse;
import com.app.studyroom.db.AreaInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SeatMapper {

    // 后端 seatPlace ↔ 前端 roomId
    private static final Map<String, String> backendRoomToFrontend = new HashMap<>();
    private static final Map<String, String> frontendRoomToBackend = new HashMap<>();

    // 后端 seatNum ↔ 前端座位 id（int）
    private static final Map<String, Integer> backendSeatNumToFrontendId = new HashMap<>();
    private static final Map<Integer, String> frontendIdToBackendSeatNum = new HashMap<>();

    static {
        // 教室号默认映射
        backendRoomToFrontend.put("501", "501自习室");
        backendRoomToFrontend.put("102", "A校区02自习室");
        backendRoomToFrontend.put("103", "A校区03自习室");
        backendRoomToFrontend.put("201", "B校区01自习室");
        backendRoomToFrontend.put("202", "B校区02自习室");

        for (Map.Entry<String, String> entry : backendRoomToFrontend.entrySet()) {
            frontendRoomToBackend.put(entry.getValue(), entry.getKey());
        }

        // 座位号映射（001 ↔ 1）
        for (int i = 1; i <= 1000; i++) {
//            String seatNum = String.format("%03d", i);  //前三位自动补零
            String seatNum = String.valueOf(i);
            backendSeatNumToFrontendId.put(seatNum, i);
            frontendIdToBackendSeatNum.put(i, seatNum);
        }
    }


    public static String getFrontendRoom(String backendRoomId) {
        return backendRoomToFrontend.get(backendRoomId);
    }

    public static String getBackendRoom(String frontendRoomId) {
        return frontendRoomToBackend.get(frontendRoomId);
    }

    public static Integer getFrontendSeatId(String backendSeatNum) {
        return backendSeatNumToFrontendId.get(backendSeatNum);
    }

    public static String getBackendSeatNum(int frontendSeatId) {
        return frontendIdToBackendSeatNum.get(frontendSeatId);
    }
    // 添加一个新的方法，支持从 treeQy 接口初始化
    public static void updateRoomMappingFromClassroomTree(List<ClassroomResponse> treeList) {
        backendRoomToFrontend.clear();
        frontendRoomToBackend.clear();

        for (ClassroomResponse area : treeList) {
            if ("1".equals(area.cidNum)) {  // 父区域
                if (area.children != null) {
                    for (ClassroomResponse room : area.children) {
                        if ("2".equals(room.cidNum) && room.qyName != null) {
                            backendRoomToFrontend.put(String.valueOf(room.id), room.qyName);
                            frontendRoomToBackend.put(room.qyName, String.valueOf(room.id));
                        }
                    }
                }
            }
        }
    }
    public static String getRoomIdMapAsString() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : frontendRoomToBackend.entrySet()) {
            sb.append(entry.getKey()).append(" → ").append(entry.getValue()).append("; ");
        }
        return sb.toString();
    }


}
